package com.thiswhyme.demo.msgpush.core.impl.websocket;

import com.thiswhyme.common.expection.BusinessException;
import com.thiswhyme.demo.msgpush.core.WsConstant;
import com.thiswhyme.demo.msgpush.core.config.MsgPushProperties;
import com.thiswhyme.demo.msgpush.core.impl.websocket.event.BroadcastEventWsHandler;
import com.thiswhyme.demo.msgpush.core.impl.websocket.event.CloseEventWsHandler;
import com.thiswhyme.demo.msgpush.core.impl.websocket.event.ConnectEventWsHandler;
import com.thiswhyme.demo.msgpush.core.impl.websocket.event.PushEventWsHandler;
import com.thiswhyme.redis.subscribe.RedisPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import java.util.Optional;

/**
 * @description: WebSocket实现配置
 * @author: thiswhyme
 * @date: 2021/9/27
 */
@ConditionalOnProperty(value = "msg-push.websocket.impl", havingValue = WsConstant.IMPL_WS)
@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer {

    @Lazy @Qualifier("defaultRedisPublisher")
    @Autowired
    private RedisPublisher redisPublisher;

    @Autowired
    private MsgPushProperties msgPushProperties;

//    public WebSocketConfiguration(@Lazy @Qualifier("defaultRedisPublisher") RedisPublisher redisPublisher,
//                                  MsgPushProperties msgPushProperties) {
//        this.redisPublisher = redisPublisher;
//        this.msgPushProperties = msgPushProperties;
//    }

    @Bean
    public DefaultWebSocketHandler defaultWebSocketHandler() {
        return new DefaultWebSocketHandler(redisPublisher);
    }

    @Bean
    public DefaultHandshakeInterceptor defaultHandshakeInterceptor() {
        return new DefaultHandshakeInterceptor();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        MsgPushProperties.WsProperties wsProperties = Optional.ofNullable(msgPushProperties.getWsProperties())
                .orElseThrow(() -> new BusinessException("WS配置不完整"));
        registry
                .addHandler(defaultWebSocketHandler(), wsProperties.getPath())
                .addInterceptors(defaultHandshakeInterceptor())
                .setAllowedOrigins("*");
    }

    @Bean
    public BroadcastEventWsHandler broadcastEventWsHandler() {
        return new BroadcastEventWsHandler();
    }

    @Bean
    public CloseEventWsHandler closeEventWsHandler() {
        return new CloseEventWsHandler();
    }

    @Bean
    public ConnectEventWsHandler connectEventWsHandler() {
        return new ConnectEventWsHandler();
    }

    @Bean
    public PushEventWsHandler pushEventWsHandler() {
        return new PushEventWsHandler();
    }
}
